#!/bin/bash

tidb_host='{{ params.tidb_host }}'
tidb_port='{{ params.tidb_port }}'
tidb_url='{{ params.tidb_url }}'
tidb_user='{{ params.tidb_user }}'
tidb_password='{{ params.tidb_password }}'

hive_table='{{ params.hive_table }}'
tidb_table='{{ params.tidb_table }}'
if_partitions='{{ params.if_partitions }}'
interval_dt='{{ params.interval_dt }}'
sqoop_maps='{{ params.sqoop_maps }}'
sqoop_execution_date='{{ execution_date | cst_ds }}'

start_date='{{ execution_date | date_add(1) | cst_ds_nodash }}'


# 参数校验
params_check() {
# 判断传入参数规范性
if [ $(awk -F '.' '{print NF}' <<< "${hive_table}") -ne 2 ]; then echo "Param Error : ${hive_table} naming is not standardized !!!";exit 1; fi

if [ $(awk -F '.' '{print NF}' <<< "${tidb_table}") -ne 2 ]; then echo "Param Error : ${tidb_table} naming is not standardized !!!";exit 1; fi

if [ "${if_partitions}" != "true" ] && [ "$if_partitions" != "false" ]; then echo "Param Error : if_partitions not in 'true' or 'false' !!! "; exit 1; fi

if ! [[ ${interval_dt} =~ ^[1-9][0-9]*$ ]]; then echo "Param Error : interval_dt is not standardized !!! ";exit 1; fi

if [ "${sqoop_maps}" -gt 80 ]; then echo "Param Error : sqoop map is too big !!! "; exit 1; fi

}

# 清空表分区数据
truncate_partitions() {

    partitions=()
    for ((i=0; i<interval_dt; i++)); do
        current_date=$(date -d "$start_date -$i days" +%Y%m%d)
        partition_name="p${current_date}"
        partitions+=($partition_name)
    done

    partitions_list=$(IFS=,; echo "${partitions[*]}")
    alter_statement="mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p${tidb_password}  -e  \"ALTER TABLE ${tidb_table} TRUNCATE PARTITION ${partitions_list} ; \" "
    echo 'Tidb Truncate SQL : ' ${alter_statement}
    eval $alter_statement && echo "Tidb truncate partitions command executed successfully~~~" || { echo "Tidb truncate partitions command execution failed!!!"; exit 1; }


}
# 清空表数据
truncate_table() {

    alter_statement="mysql -h ${tidb_host} -P ${tidb_port}  -u ${tidb_user} -p${tidb_password}  -e \" TRUNCATE TABLE ${tidb_table} ; \" "
    echo 'Tidb Truncate SQL : ' ${alter_statement}
    eval $alter_statement && echo "Tidb truncate table command executed successfully~~~" || { echo "Tidb truncate table command execution failed!!!"; exit 1; }


}

# 导入数据
export_to_tidb() {
	sqoop_shell="sqoop export \
	-Dsqoop.export.records.per.statement=100 \
	-Dsqoop.export.statements.per.transaction=10 \
	--connect                     ${tidb_url} \
	--username                    ${tidb_user} \
	--password                    ${tidb_password} \
	--table                       ${tidb_table#*.} \
	--hcatalog-database           ${hive_table%%.*} \
	--hcatalog-table              ${hive_table#*.} \
  --batch \
	-m ${sqoop_maps} "
  echo 'Sqoop Shell : ' ${sqoop_shell}
	eval ${sqoop_shell} && echo "Sqoop export  executed successfully~~~" || { echo "Sqoop export  executed failed!!!"; exit 1; }
}

main() {
# 校验参数
params_check
# 执行清空+写入数据
if [ 'true' = ${if_partitions} ]; then truncate_partitions;export_to_tidb; else truncate_table;export_to_tidb; fi
}

main